<?php

class Tourism_goods_category_additive_model extends CI_Model
{
    private $table = 'tourism_goods_category_additive';
    
    public function total($params = array())
    {
        if (!empty($params['name'])) {
            $this->db->where('name', $params['name']);
        }
        $this->db->where('category_id', $params['category_id']);
        $this->db->where('status', 1);
        return $this->db->count_all_results($this->table);
    }

    public function page_list($page_num, $num, $params = array())
    {
        if (!empty($params['name'])) {
            $this->db->where('name', $params['name']);
        }
        $this->db->where('category_id', $params['category_id']);
        $this->db->where('status', 1);
        $this->db->limit($page_num, $num);
        return $this->db->get($this->table);
    }

    /*
     * 附加属性增加
     * */
    public function insertTourismAdditive($postData)
    {
        $data = array(
            'category_id' => $postData['category_id'],
            'name'        => $postData['name']
        );
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }
    
    public function insertBatchAdditive($postData)
    {
        $data = array();
        $name = preg_replace("/(\n)|(\s)|(\t)|(\')|(')|(，)|(\.)/", ',', $postData['name']);
        $additive = explode(',', $name);
        if (!empty($additive) > 0) {
            foreach ($additive as $key => $val) {
                $data[$key]['category_id'] = $postData['category_id'];
                $data[$key]['name']        = $val;
            }
            return $this->db->insert_batch($this->table, $data);
        }
        return false;
    }

    /*
     * 根据id获取附加属性信息
     * */
    public function getAdditiveById($id)
    {
        $this->db->where('id', $id);
        return $this->db->get($this->table);
    }

    /*-----------------------------*/
    public function updateTourismAdditive($params)
    {
        $data = array(
            'name' => $params['name'],
        );
        $this->db->where('id', $params['id']);
        return $this->db->update($this->table, $data);
    }

    public function delete($id)
    {
        $this->db->where('id', $id);
        return $this->db->delete($this->table);
    }

    public function getAdditive($category_id)
    {
        $this->db->where('category_id', $category_id);
        return $this->db->get($this->table);
    }

}